package com.zhaoyu.wemedia.controller;

import com.zhaoyu.apis.wemedia.WmNewsControllerApi;
import com.zhaoyu.model.admin.dtos.NewsAuthDto;
import com.zhaoyu.model.common.dtos.ResponseResult;
import com.zhaoyu.model.common.enums.AppHttpCodeEnum;
import com.zhaoyu.model.media.dtos.WmNewsDto;
import com.zhaoyu.model.media.dtos.WmNewsPageReqDto;
import com.zhaoyu.model.media.pojos.WmNews;
import com.zhaoyu.model.media.vo.WmNewsCheckVO;
import com.zhaoyu.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;

    @Override
    @PostMapping("/upload_picture") // json格式的数据
    public ResponseResult uploadImageWmNews(MultipartFile file) { // 测试的时候不要在请求头中添加Content-Type: application/json(切记！！！)
        return wmNewsService.uploadImage(file);
    }

    @Override
    @PostMapping("/findAll") // json格式的数据
    public ResponseResult findWmNews(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    @Override
    @PostMapping("/submit") // json格式的数据
    public ResponseResult summitWmNews(@RequestBody WmNewsDto wmNewsDto) {
        /**
         * 当前状态
             0 草稿 NORMAL
             1 提交(待审核) SUBMIT
             2 审核失败 FAIL
             3 人工审核 ADMIN_AUTH
             4 人工审核通过 ADMIN_SUCCESS
             8 审核通过(待发布) SUCCESS
             9 已发布 PUBLISHED
         */
        if (wmNewsDto.getStatus() == WmNews.Status.SUBMIT.getCode()) {
            // 提交文章
            return wmNewsService.saveWmNews(wmNewsDto,WmNews.Status.SUBMIT.getCode());
        }else {
            // 保存草稿
            return wmNewsService.saveWmNews(wmNewsDto,WmNews.Status.NORMAL.getCode());
        }
    }

    @Override
    @GetMapping("/findById/{id}") // rest风格
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    @Override
    @GetMapping("/delete/{id}") // rest风格
    public ResponseResult deleteWmNews(@PathVariable("id") Integer id) {
        return wmNewsService.deleteWmNews(id);
    }

    @Override
    @PostMapping("/down_or_up") // json格式的数据
    public ResponseResult downOrUpWmNews(@RequestBody WmNewsDto wmNewsDto) {
        return wmNewsService.downOrUpWmNews(wmNewsDto);
    }

    @Override
    @GetMapping("/findByIdSTL/{id}") // rest风格
    public WmNews findWmNewsByIdSTL(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @Override
    @PostMapping("/update") // json格式的数据
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        boolean b = wmNewsService.updateById(wmNews);
        if (b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    @GetMapping("/findRelease")
    public List<Integer> findReleaseWmNews() {
        return wmNewsService.findReleaseWmNews();
    }

    @Override
    @PostMapping("/findList") // json格式的数据
    public ResponseResult findListWmNews(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.findListWmNews(newsAuthDto);
    }

    @Override
    @GetMapping("/findWmNewsCheckVO/{id}") // rest风格
    public WmNewsCheckVO findWmNewsCheckVO(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsCheckVO(id);
    }

}